import BaseModel from "../../cfw/mvc/BaseModel";
import User from "../db/User";
import ClotheManager from "../dressroom/model/ClotheManager";
import ModelManager from "../../cfw/mvc/ModelManager";
import GrowItemModel from "./model/GrowItemModel";
import StateItemModel from "./model/StateItemModel";
import ModuleManager from "../../cfw/module/ModuleManager";
import GlobalEvent from "../../cfw/event/GlobalEvent";
import { EventName } from "../../public/Config";
import PlayerController from "./PlayerController";



export default class Player extends BaseModel {




    protected clotheList: number[] = []

    private static ins: Player;

    protected growMgr: ModelManager<GrowItemModel> = new ModelManager()

    protected stateMgr: ModelManager<StateItemModel> = new ModelManager()


    // protected observer: TimeObserver = new TimeObserver(this.updateTime.bind(this), 60)

    static instance() {
        if (!this.ins) {
            this.ins = new Player()
        }
        return this.ins;
    }

    private level: number = 0;
    private gameTime: number = 0;
    // protected stateTime: number[] = []
    constructor() {
        super();
        this.level = User.instance().getLevel()
        this.gameTime = User.instance().getGameTime()
        this.stateMgr.initWithData(ModuleManager.dataManager.get(StateItemModel.CLASS_NAME), StateItemModel)
        this.growMgr.initWithData(ModuleManager.dataManager.get(GrowItemModel.CLASS_NAME), GrowItemModel)
        // let count = this.stateMgr.size()
        // for (let index = 0; index < count; index++) {
        //     this.stateTime[index] = User.instance().getPlayerStateValue(index)
        // }
        // GlobalTimeManager.instance().addTimeListener(this.observer)
    }

    getStateItemModel(id: number) {
        return this.stateMgr.getByID(id)
    }


    // getStateTime(type: number) {
    //     return this.stateTime[type]
    // }

    setStateTime(typeID: number, time: number) {

        this.stateMgr.getByID(typeID).setState(time)
    }

    private minID: number = 0;
    private isTipClose: boolean = true;

    // 一分钟走一次。
    updateTime(time: number) {
        // cc.log(' udpateTime =================  ', time)
        let min: number = -1;
        let minID: number = 0;
        let list: StateItemModel[] = this.stateMgr.getList()
        for (let index = 0; index < list.length; index++) {
            list[index].updateNum()
            let item = list[index]
            if (min == -1) {
                min = item.getPercent()
                minID = item.getID()
            } else {
                if (item.getPercent() < min) {
                    min = item.getPercent();
                    minID = item.getID()
                }
            }
            // this.stateTime[index] -= list[index].getSubnum()
            // if (this.stateTime[index] < 0) {
            //     this.stateTime[index] = 0;
            //     User.instance().setPlayerStateValue(index, this.stateTime[index])
            // }
        }
        if (min >= 0) {
            if (min <= 0.2) {
                if (this.minID != minID) {
                    this.minID = minID;
                    //     if (this.isTipClose) {
                    //         GlobalEvent.instance().emit(EventName.OPEN_GAME_TIP)
                    //         this.isTipClose = false;
                    //     }
                }
                GlobalEvent.instance().emit(EventName.CHANGE_GAME_TIP, this.minID)
            }
        } else {
            // if (!this.isTipClose) {
            GlobalEvent.instance().emit(EventName.CLOSE_GAME_TIP, this.minID)
            // this.isTipClose = true;
            // }

        }

        // this.updateGrow(1)

    }


    updateExp(state: StateItemModel) {
        if (state) {
            this.updateGrow(state.getExp())
        }
    }

    updateGrow(num: number) {
        let grow: GrowItemModel = this.growMgr.getByID(this.level)
        if (grow.getTime() > 0) {
            this.gameTime += num;
            if (this.gameTime >= grow.getTime()) {
                this.gameTime = 0;
                this.updateLevel()
            }
            User.instance().setGameTime(this.gameTime)
        }
    }

    getGrowItemModel() {
        cc.log(' this.level ', this.level)
        let grow: GrowItemModel = this.growMgr.getByID(this.level)
        return grow
    }

    getLevel() {
        return this.level;
    }

    updateLevel() {
        if (this.level < this.growMgr.size()) {
            this.level++;
            User.instance().setLevel(this.level)
            PlayerController.instance().intoLayer()
        } else {

        }

    }

    getClothe(id: number) {
        return ClotheManager.instance().getClothe(id)
    }

    getClotheList() {
        this.clotheList.length = 0;
        let list = User.instance().getTypeData();
        // cc.log(' Player start list ',list)
        for (let index = 0; index < list.length; index++) {
            const element = list[index];
            this.clotheList[index] = element;
        }
        return this.clotheList;
    }

    canDel(index: number) {
        return ClotheManager.instance().getClotheType(index + 1).getCanDel() == 1
    }


}
